package com.zyx.controller;

/**
 * @author zyx
 * @date 2022/1/8
 */

import com.github.pagehelper.PageInfo;
import com.zyx.pojo.*;
import com.zyx.service.*;
import com.zyx.vo.GetSession;
import com.zyx.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("Spencer")
@ResponseBody
public class SpencerController {
    @Resource
    private SpencerService spencerService;
    @Resource
    private AutoService autoService;
    @Resource
    private OrderService orderService;
    @Resource
    private RelevancyService relevancyService;
    @Resource
    private NameService nameService;


    /**
     * 查看汽车数据
     */
    @ResponseBody
    @RequestMapping(value = "AutoSize", method = RequestMethod.GET)
    public Map<String, Integer> queryAllAuto(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        GetSession getSession = new GetSession();
        Spencer spencer = getSession.spencerSession(request, response, session);
        List<Relevancy> r = relevancyService.queryByType(spencer.getSpencerId());
        int i = relevancyService.queryByCount(spencer.getSpencerId());
        Map<String, Integer> map = new HashMap<>();
        map.put("autoSize", i);
        map.put("typeSize",r.size());
        return map;
    }

    /**
     * @Description: 总预约数,新增预约数
     * @Author: zyx
     * @Date: 16:32 2022/2/12
     * @Name: queryAllOrders
     * @Param: [request, response, session]
     * @Return: java.util.Map<java.lang.String, java.lang.Integer>
     */
    @ResponseBody
    @RequestMapping(value = "OrderSize", method = RequestMethod.GET)
    public Map<String, Integer> queryAllOrders(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Map<String, Integer> map = orderService.queryAllSpencer(request, response, session);
        return map;
    }

    /**
     * @Description: 显示店铺姓名
     * @Author: zyx
     * @Date: 19:42 2022/2/14
     * @Name: querySpencer
     * @Param: [request, response, session]
     * @Return: com.zyx.pojo.Spencer
     */
    @ResponseBody
    @RequestMapping(value = "user", method = RequestMethod.POST)
    public Spencer querySpencer(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        GetSession getSession = new GetSession();
        Spencer spencer = getSession.spencerSession(request, response, session);
        return spencer;
    }


    /**
     * @Description: 根据信息查询车俩
     * @Author: zyx
     * @Date: 20:46 2022/2/15
     * @Name: queryAllScreen
     * @Param: [pageNum, pageSize, auto, request, response, session]
     * @Return: com.zyx.vo.ResultVo<com.zyx.pojo.Relevancy>
     */
    @ResponseBody
    @RequestMapping(value = "queryAllScreen",method = RequestMethod.POST)
    public ResultVo<Relevancy> queryAllScreen(Integer pageNum, Integer pageSize, Auto auto, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession getSession = new GetSession();
        Relevancy relevancy = new Relevancy();
        System.out.println(auto);
        Spencer spencer = getSession.spencerSession(request, response, session);
        relevancy.setSpencerId(spencer.getSpencerId());
        relevancy.setAuto(auto);
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Relevancy> relevancyPageInfo = relevancyService.queryAllScreen(pageNum, pageSize, relevancy);
        return new ResultVo<>(relevancyPageInfo);
    }

    //获取店铺汽车类型信息
    @ResponseBody
    @RequestMapping(value = "queryByType",method = RequestMethod.GET)
    public ResultVo<Relevancy> queryByType(Integer pageNum, Integer pageSize, Auto auto, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession getSession = new GetSession();
        Relevancy relevancy = new Relevancy();
        Spencer spencer = getSession.spencerSession(request, response, session);
        relevancy.setSpencerId(spencer.getSpencerId());
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Relevancy> relevancyPageInfo = relevancyService.queryByType(pageNum, pageSize, relevancy);
        return new ResultVo<>(relevancyPageInfo);
    }

    //根据汽车类型获取店铺汽车详细信息
    @ResponseBody
    @RequestMapping(value = "queryByAuto",method = RequestMethod.GET)
    public ResultVo<Relevancy> queryByAuto(Integer pageNum, Integer pageSize, Relevancy relevancy, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession getSession = new GetSession();
        System.out.println(relevancy);
        Spencer spencer = getSession.spencerSession(request, response, session);
        relevancy.setSpencerId(spencer.getSpencerId());
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Relevancy> relevancyPageInfo = relevancyService.queryByAuto(pageNum,pageSize,relevancy);
        return new ResultVo<>(relevancyPageInfo);
    }

    //查询车辆所有品牌
    @ResponseBody
    @RequestMapping(value = "queryByFirm",method = RequestMethod.GET)
    public List<String> queryByFirm(){
        return nameService.queryByFirm();
    }

    //查询汽车名称和id
    @ResponseBody
    @RequestMapping(value = "queryByAutoId",method = RequestMethod.POST)
    public List<Name> queryByAutoId(String firm){
        return nameService.queryByAutoId(firm);
    }

    //店铺添加车辆
    @ResponseBody
    @RequestMapping(value = "addRelevancy",method = RequestMethod.POST)
    public ResultVo addRelevancy(Relevancy relevancy,HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession g = new GetSession();
        Spencer s = g.spencerSession(request,response,session);
        relevancy.setSpencerId(s.getSpencerId());
        System.out.println(relevancy);
        List<Relevancy> list = relevancyService.exists(relevancy);
        if (list.size()>0){
            return new ResultVo(500,"现在店铺已有这种车辆");
        }
        int i = relevancyService.addRelevancy(relevancy);
        if (i>0){
            return new ResultVo();
        }
        return new ResultVo(500,"添加失败");
    }

    //查询汽车型号和id
    @ResponseBody
    @RequestMapping(value = "queryByModel",method = RequestMethod.GET)
    public List<Name> queryByModel(Integer id){
        return nameService.queryByModel(id);
    }

    //删除店铺中的车辆信息
    @ResponseBody
    @RequestMapping(value = "deleteByAutoId",method = RequestMethod.DELETE)
    public ResultVo deleteByAutoId(Integer autoId,Integer carId,HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession g = new GetSession();
        Spencer s = g.spencerSession(request,response,session);
        Relevancy relevancy = new Relevancy();
        relevancy.setSpencerId(s.getSpencerId());
        relevancy.setAutoId(autoId);
        relevancy.setCarId(carId);
        Integer i = relevancyService.deleteByAutoId(relevancy);
        if (i>0){
            return new ResultVo();
        }
        return new ResultVo(500,"删除失败！");
    }

    //删除店铺中的车辆信息
    @ResponseBody
    @RequestMapping(value = "deleteByCarId",method = RequestMethod.DELETE)
    public ResultVo deleterByCarId(Integer carId,HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession g = new GetSession();
        Spencer s = g.spencerSession(request,response,session);
        Relevancy relevancy = new Relevancy();
        relevancy.setSpencerId(s.getSpencerId());
        relevancy.setCarId(carId);
        System.out.println(relevancy);
        Integer i = relevancyService.deleteByCarId(relevancy);
        if (i>0){
            return new ResultVo();
        }
        return new ResultVo(500,"");
    }

    //根据主键更新车辆信息
    /*@ResponseBody
    @RequestMapping(value = "auto/{id}", method = RequestMethod.PUT)
    public ResultVo<Auto> updateAuto(@PathVariable("id") Integer autoId, Auto auto) {
        auto.setAutoId(autoId);
        int i = autoService.updateAuto(auto);
        if (i == 1) {
            return new ResultVo<Auto>();
        }
        return new ResultVo<>(500, "添加失败！请稍后再试！");
    }*/

    //添加车辆信息
    /*@ResponseBody
    @RequestMapping(value = "addAuto", method = RequestMethod.POST)
    public ResultVo<Auto> addAuto(Auto auto,HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        auto.setTimeMarket(new Date());
        int i = autoService.addAuto(auto);
        if (i == 1) {
            GetSession getSession = new GetSession();
            Spencer spencer = getSession.spencerSession(request, response, session);
            Relevancy relevancy = new Relevancy();
            relevancy.setSpencerId(spencer.getSpencerId());
            Auto auto1 = autoService.selectId(new Date());
            relevancy.setAutoId(auto1.getAutoId());
            int i1 = relevancyService.addRelevancy(relevancy);
            if (i1==1) {
                return new ResultVo<>();
            }
        }
        return new ResultVo<>(500, "添加失败！稍后再试！");
    }*/

    //根据主键查询车辆信息
    /*@ResponseBody
    @RequestMapping(value = "selectKey", method = RequestMethod.GET)
    public Auto selectKey(int id) {
        Auto auto = autoService.selectByKey(id);
        return auto;
    }*/

    //根据店铺信息和车辆id删除店铺管理某车辆权限
    /*@ResponseBody
    @RequestMapping(value = "auto/{id}", method = RequestMethod.DELETE)
    public ResultVo<Spencer> deleteAuto(@PathVariable("id") Integer autoId,HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        GetSession getSession = new GetSession();
        Spencer spencer = getSession.spencerSession(request, response, session);
        int i = relevancyService.deleteByExample(spencer.getSpencerId(), autoId);
        if (i == 1) {
            return new ResultVo<>();
        }
        return new ResultVo<>(500, "删除失败！请稍后再试！");
    }*/

    //上传汽车图片并更新图片信息
    /*@ResponseBody
    @RequestMapping(value = "autoLogo/{id}")
    public ResultVo<Auto> uploadLogo(@RequestParam("logo") MultipartFile myFile, HttpServletRequest request, @PathVariable("id") Integer id) {
        //文件上传到指定服务器位置
        String path = request.getServletContext().getRealPath("/assets/images/auto/");
        //获取文件名称
        String originalFilename = myFile.getOriginalFilename();
        //定义新的文件名称：随机生成名称+文件后缀名
        String randomName = UUID.randomUUID().toString().replace("-", "");
        int i = originalFilename.lastIndexOf(".");
        String hz = originalFilename.substring(i);
        String logoName = randomName + hz;
        int num = 0;
        try {
            myFile.transferTo(new File(path + "/" + logoName));
            System.out.println("上传成功！" + path + "/" + logoName);
            Auto auto = new Auto();
            auto.setAutoId(id);
            auto.setPicture(logoName);
            Auto a = autoService.selectByKey(id);
            //将图片名称更新到数据库中
            num = autoService.updateAuto(auto);
            //将原有图片删除
            if (a.getPicture() != null) {
                File file = new File("D:\\Text\\cat\\target\\cat-1.0-SNAPSHOT\\assets\\images\\auto\\" + a.getPicture());
                file.delete();
            }
            if (num == 1) {
                return new ResultVo<Auto>();
            } else {
                return new ResultVo<>(500, "上传失败！请稍后再试！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVo<>(500, "图片上传失败！" + e.getMessage());
        }
    }*/

    //上传店铺图片并更新图片信息
    @ResponseBody
    @RequestMapping(value = "spencerLogo")
    public ResultVo<Spencer> upSpencerLogo(@RequestParam("logo") MultipartFile myFile, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        GetSession getSession = new GetSession();
        Spencer spencer = getSession.spencerSession(request, response, session);
        //文件上传到指定服务器位置
        String path = request.getServletContext().getRealPath("/assets/images/spencer/");
        //获取文件名称
        String originalFilename = myFile.getOriginalFilename();
        //定义新的文件名称：随机生成名称+文件后缀名
        String randomName = UUID.randomUUID().toString().replace("-", "");
        int i = originalFilename.lastIndexOf(".");
        String hz = originalFilename.substring(i);
        String logoName = randomName + hz;
        int num = 0;
        try {
            myFile.transferTo(new File(path + "/" + logoName));
            System.out.println("上传成功！" + path + "/" + logoName);
            spencer.setPicture(logoName);
            Spencer s = spencerService.selectByKey(spencer.getSpencerId());
            //将图片名称更新到数据库中
            num = spencerService.updateSpencer(spencer);
            //将原有图片删除
            if (s.getPicture() != null) {
                File file = new File("D:\\Text\\cat\\target\\cat-1.0-SNAPSHOT\\assets\\images\\spencer\\" + s.getPicture());
                file.delete();
            }
            if (num == 1) {
                return new ResultVo<Spencer>();
            } else {
                return new ResultVo<>(500, "上传失败！请稍后再试！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVo<>(500, "图片上传失败！" + e.getMessage());
        }

    }

    //预约人信息分页查询
    @ResponseBody
    @RequestMapping(value = "listOrders", method = RequestMethod.POST)
    public ResultVo<Orders> queryByPage(Integer pageNum, Integer pageSize, Orders orders,HttpServletRequest request,HttpServletResponse response, HttpSession session) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        GetSession getSession = new GetSession();
        Spencer spencer = getSession.spencerSession(request, response, session);
        orders.setSpencerId(spencer.getSpencerId());
        PageInfo<Orders> teamPageInfo = orderService.queryAll(pageNum, pageSize, orders);//调用分页查询和模糊查询
        return new ResultVo<>(teamPageInfo);
    }

    //根据主键更新店铺信息
    @ResponseBody
    @RequestMapping(value = "spencer", method = RequestMethod.PUT)
    public ResultVo<Spencer> updateSpencer( HttpServletRequest request, HttpServletResponse response, HttpSession session,Spencer spencer) {
        GetSession getSession = new GetSession();
        Spencer s = getSession.spencerSession(request, response, session);
        spencer.setSpencerId(s.getSpencerId());
        int i = spencerService.updateSpencer(spencer);
        if (i == 1) {
            return new ResultVo<Spencer>();
        }
        return new ResultVo<>(500, "添加失败！请稍后再试！");
    }

    //根据主键查询店铺信息
    @ResponseBody
    @RequestMapping(value = "selectKeySpencer", method = RequestMethod.POST)
    public Spencer selectKeySpencer(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        GetSession getSession = new GetSession();
        int i = getSession.spencerSession(request, response, session).getSpencerId();
        return spencerService.selectByKey(i);
    }

    //根据主键信息修改预约信息，联系预约用户
    @ResponseBody
    @RequestMapping(value = "update/{id}", method = RequestMethod.PUT)
    public ResultVo<Orders> updateOrders(@PathVariable("id") Integer orderId){
        Orders orders = new Orders();
        orders.setOrderId(orderId);
        orders.setState(1);
        System.out.println(orderId);
        int i = orderService.updateOrder(orders);
        if (i>0){
            return new ResultVo<>();
        }
        return new ResultVo<>(500,"错误，请联系系统管理员");
    }
}
